package com.lap.framework.junit.domain.methods;

import static com.lap.framework.junit.shared.tool.Constant.*;

import com.lap.framework.junit.domain.valueobject.JField;
import com.lap.framework.junit.domain.valueobject.JMethod;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;

public class GetMethod implements AbstractMethod {

  @Override
  public String execute(JMethod methodInfo) {
    StringBuilder result = new StringBuilder(128);

    result.append(TAB2).append(formatter(PERFORM_PRE, "get", methodInfo.getUri()));
    if (methodInfo.isPathVariable()) {
      String parameter =
          methodInfo.getFields().stream().map(JField::getDataName).collect(Collectors.joining(","));

      if (StringUtils.isNotBlank(parameter)) result.append(", ").append(parameter);

      result.append(")) //perform").append(WRAP);
    } else if (methodInfo.isArgMap()) {
      result.append(")");
      methodInfo
          .getFields()
          .forEach(
              item -> {
                result
                    .append(WRAP)
                    .append(TAB6)
                    .append(TAB2)
                    .append(".param(\"")
                    .append(item.getDataName())
                    .append("\"");
                result.append(", map.get(\"").append(item.getDataName()).append("\"))");
              });
      result.append(") //perform").append(WRAP);
    } else {
      result.append(")");
      methodInfo
          .getFields()
          .forEach(
              item -> {
                result.append(".param(\"").append(item.getDataName()).append("\"");
                result.append(", ").append(item.getDataName()).append(")");
              });
      result.append(") //perform").append(WRAP);
    }
    result.append(TAB6).append(EXPECT_STATUS_IS_OK).append(WRAP);
    result.append(TAB6).append(formatter(EXPECT_JSON, getCode(methodInfo))).append(WRAP);

    return result.toString();
  }
}
